<?php
/**
 * Created by PhpStorm.
 * User: l-eru
 * Date: 2018/7/5
 * Time: 9:57
 */

namespace Core;


/**
 * Class Log
 * @package Core
 * @method self info(array|string $message, array $content = [])
 * @method self debug(array|string $message, array $content = [])
 * @method self notice(array|string $message, array $content = [])
 * @method self warning(array|string $message, array $content = [])
 * @method self error(array|string $message, array $content = [])
 * @method self critical(array|string $message, array $content = [])
 * @method self alert(array|string $message, array $content = [])
 * @method self emergency(array|string $message, array $content = [])
 */
class Log
{
    // 系统日志存放基础路径
    protected const LOG_PATH = ROOT . '/storage/logs/';

    public const CONFIG_SERVER  = 'config'; // 控制服务器
    public const DATA_SERVER    = 'data';   // 数据服务器

    protected $device;
    protected $type;


    protected $module;

    /**
     * @var self $instance
     */
    protected static $instance = null;

    protected function __construct()
    {
        \SeasLog::setBasePath(self::LOG_PATH);
    }

    public static function getInstance($device, $type)
    {
        if (empty(self::$instance)) {
            self::$instance = new self();
        }

        self::$instance->setModule($device, $type);

        return self::$instance;
    }

    public function setModule($device, $type)
    {
        $path = $type . date('/Y-m/d/') . $device;

        $this->module = $path;
    }

    public function log($level, $message, $content = [])
    {
        if (is_string($message)) {
            $message = explode('|', $message);

            $msg[$message[0]] = $message[1];
            $message = $msg;
        }

        \SeasLog::log($level, json_encode($message), $content, $this->module);

        return $this;
    }

    public function __call($name, $arguments)
    {
        // TODO: Implement __call() method.
        $level = constant('SEASLOG_' . strtoupper($name));

        array_unshift($arguments, $level);
        return call_user_func_array([$this, 'log'], $arguments);
    }
}